package id.co.shuba.spamobile.widget;

import id.co.shuba.spamobile.R;
import id.co.shuba.spamobile.activities.FormDSARActivity;
import id.co.shuba.spamobile.activities.PipelineViewActivity;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Typeface;
import android.opengl.Visibility;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

public class DSARCursorAdapter extends CursorAdapter {
	private Context mContext;

	public class ViewHolder {
		TextView txtDateVisitPlan;
		TextView txtObjectiveOfVisit;
		ImageButton btnDelete;
	}

	public DSARCursorAdapter(Context context, Cursor c, int flags) {
		super(context, c, flags);
		mContext = context;
	}

	public DSARCursorAdapter(Context context, Cursor c, boolean autoRequery) {
		super(context, c, autoRequery);
		mContext = context;
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		View view = LayoutInflater.from(mContext)
				.inflate(R.layout.dsar_item , parent, false);
		ViewHolder viewHolder = new ViewHolder();
		viewHolder.txtDateVisitPlan = (TextView) view.findViewById(R.id.txtDateVisitPlan);
		viewHolder.txtObjectiveOfVisit = (TextView) view.findViewById(R.id.txtObjectiveOfVisit);
		viewHolder.btnDelete = (ImageButton) view.findViewById(R.id.btnDelete);
		view.setTag(viewHolder);
		return view;
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		ViewHolder viewHolder = (ViewHolder) view.getTag();		
		viewHolder.txtDateVisitPlan.setText(cursor.getString(cursor.getColumnIndex("DATE_VISIT_PLAN")));
		
		String objective = cursor.getString(cursor.getColumnIndex("OBJECTIVE_OF_VISIT"));
		if (objective == null || "".equals(objective)) {			
			objective = "Belum ada aktual";
			viewHolder.txtObjectiveOfVisit.setTypeface(null, Typeface.BOLD_ITALIC);					
		} else {
			viewHolder.txtObjectiveOfVisit.setTypeface(null, Typeface.NORMAL);			
		}		
		
		//Ini ngaco bgt reference ke spesifik type
		//sementara dulu
		PipelineViewActivity act = (PipelineViewActivity) context;
		if (!act.isProspectClosed() && cursor.getPosition() == cursor.getCount() - 1) {
			viewHolder.btnDelete.setVisibility(Button.VISIBLE);
			viewHolder.btnDelete.setFocusable(false);
			viewHolder.btnDelete.setTag(cursor.getInt(cursor.getColumnIndex("_id")));				
		} else {
			viewHolder.btnDelete.setVisibility(Button.GONE);
		}
					
		viewHolder.txtObjectiveOfVisit.setText(objective);		
		
	}
}
